package com.winglish.as3.player.vo 
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import com.winglish.as3.utils.ObjectUtil;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	/**
	 * ...
	 * @author charmjin
	 */
	public class ControlBar extends MovieClip
	{
		protected var barMc:MovieClip;
		protected var ballMc:MovieClip;
		protected var stateMc:MovieClip;
		
		protected var playMc:MovieClip;
		protected var pauseMc:MovieClip;
		protected var stopMc:MovieClip;
		protected var replayBtn:SimpleButton;
		
		protected var sameBtnUse:Boolean;
		protected var isDrag:Boolean = false;
		
		/**
		 * 플에이어 관련 심볼 초기화.
		 */
		public function ControlBar() 
		{
			barMc = ballMc = stateMc = barMc = null;
			playMc = pauseMc = stopMc = null;
			replayBtn = null;
		}
		
		/**
		 * 플레이어 컨트롤바 관련된 심볼들 매핑.
		 * @param	path
		 */
		protected function controlBarInit(path:MovieClip):void {
			var arr:Array = new Array();
			arr = ObjectUtil.traceDisplayList(path);
			for each (var i:String in arr) 
			{
				try {
					if (i.indexOf("Mc") != -1 ) {
						this[i] = new MovieClip();
						
					}else if (i.indexOf("Btn") != -1 ) {
						this[i] = new SimpleButton();
					}
					this[i] = path[i];
				}catch (e:Error) {
					trace(i + " not find MovieClip" );
				}
			}
		}
		
		/**
		 * 컨트롤바 무비클립 설정.
		 * @param	mc
		 */
		public function setControlBar(mc:MovieClip):void {
			if (mc) {
				controlBarInit(mc);
				
				ballMc.addEventListener(MouseEvent.MOUSE_DOWN, ballMouseDown);
				ballMc.buttonMode = true;
				
				if (sameBtnUse) {//재생 버튼과 일시정지 버튼이 하나의 무비클립으로 같이 있을 때.
					
				}else {
					if(playMc){
						playMc.addEventListener(MouseEvent.CLICK, playMcClick);
						playMc.buttonMode = true;
					}
					if(pauseMc){
						pauseMc.addEventListener(MouseEvent.CLICK, pauseMcClick);
						pauseMc.buttonMode = true;
					}
				}
				if (replayBtn) replayBtn.addEventListener(MouseEvent.CLICK, replayBtnClick);
				if (stopMc) {
					stopMc.addEventListener(MouseEvent.CLICK, stopMcClick); 
					stopMc.gotoAndStop(2);
				}else {
					if (pauseMc) {
						if (sameBtnUse) {//재생 버튼과 일시정지 버튼이 하나의 무비클립으로 같이 있을 때.
							
						}else {
							pauseMc.gotoAndStop(2);
						}
					}
				}
			}
		}
		
		/**
		 * 컨트롤바 볼 마우스 Down 이벤트.
		 * @param	e
		 */
		protected function ballMouseDown(e:MouseEvent):void {
			var rect:Rectangle = new Rectangle(0, 0, barMc.width, 0);
			var mc:MovieClip = e.currentTarget as MovieClip;
			mc.startDrag(false, rect);
			mc.addEventListener(MouseEvent.MOUSE_UP, ballMouseUp);
			mc.stage.addEventListener(MouseEvent.MOUSE_UP, ballMouseUp);
			mc.stage.addEventListener(MouseEvent.MOUSE_MOVE, ballMouseMove);
			isDrag = true;
			if (pauseMc) {
				if (sameBtnUse) {
					
				}else {
					
				}
			}
		}
		
		/**
		 * 컨트롤바 볼 마우스 Up 이벤트.
		 * @param	e
		 */
		protected function ballMouseUp(e:MouseEvent):void {
			if (isDrag) {
				ballMc.stopDrag();
				ballMc.stage.removeEventListener(MouseEvent.MOUSE_MOVE, ballMouseMove);
				isDrag = false;
			}else {
				ballMc.stage.removeEventListener(MouseEvent.MOUSE_UP, ballMouseUp);
			}
		}
		
		/**
		 * 컨트롤바 볼 마우스 move 이벤트
		 * @param	e
		 */
		protected function ballMouseMove(e:MouseEvent):void {
			if (isDrag) {
				stateMc.width = ballMc.x;
			}
		}
		
		/**
		 * play 클릭 이벤트.
		 * @param	e
		 */
		protected function playMcClick(e:MouseEvent):void {
			btnReset();
			e.currentTarget.gotoAndStop(2);
		}
		
		/**
		 * pause 클릭 이벤트.
		 * @param	e
		 */
		protected function pauseMcClick(e:MouseEvent):void {
			btnReset();
			e.currentTarget.gotoAndStop(2);
		}
		
		/**
		 * replay 클릭 이벤트.
		 * @param	e
		 */
		protected function replayBtnClick(e:MouseEvent):void {
			btnReset();
			if (sameBtnUse) {
				
			}else {
				if (playMc) playMc.gotoAndStop(2);
			}
		}
		
		/**
		 * stop 클릭 이벤트.
		 * @param	e
		 */
		protected function stopMcClick(e:MouseEvent):void {
			btnReset();
			e.currentTarget.gotoAndStop(2);
		}
		
		/**
		 * 버튼 초기화
		 */
		public function btnReset():void {
			if (sameBtnUse) {
				
			}else {
				if (playMc) playMc.gotoAndStop(1);
				if (pauseMc) pauseMc.gotoAndStop(1);
				if (stopMc) stopMc.gotoAndStop(1);
			}
		}
		
		/**
		 * 컨트롤바 이동 EnterFrame 이벤트.
		 * @param	e
		 */
		protected function enterFrame(e:Event):void {
		}
	}

}